﻿using System.Web;
using System.Xml;
using System.Collections.Generic;
using System;
using System.IO;

namespace ProlificNotion.Umbraco.MediaImporter
{
    public static class Config
    {
        private const string configFile = "/config/importMedia.config";

        private static XmlDocument xmlConfigDoc;

        /// <summary>
        /// Gets the import folders
        /// </summary>
        /// <returns>A list of folders to use for the imports</returns>
        public static List<String> ImportFolders
        {
            get
            {
                List<String> folders = new List<String>();

                foreach (XmlNode node in xmlConfigDoc.GetElementsByTagName("folder"))
                {
                    folders.Add(node.InnerText);
                }

                return folders;
            }
        }

        public static int ThumbnailMaxWidthHeight
        {
            get
            {
                int maxWidthHeight = 100;

                XmlNode node = xmlConfigDoc.SelectSingleNode("/images");
                if(node != null)
                    int.TryParse(node.Attributes["thumbnailMaxWidthHeight"].InnerText, out maxWidthHeight);

                return maxWidthHeight;
            }
        }

        /// <summary>
        /// static lazy load constructor
        /// </summary>
        static Config()
        {
            xmlConfigDoc = new XmlDocument();
            xmlConfigDoc.Load(HttpContext.Current.Server.MapPath(configFile));
        }
    }
}
